//
//  WeeksView.m
//  CalendarDemo
//
//  Created by shuai pan on 2017/1/20.
//  Copyright © 2017年 BSL. All rights reserved.
//

#import "WeeksView.h"

@interface WeeksView ()


@property (nonatomic, strong)NSMutableArray *labsArray;
@property (nonatomic, strong)UIButton *lastButton;
@property (nonatomic, strong)UIButton *nextButton;
@property (nonatomic, strong)UILabel *yearLabel;
@property (nonatomic, strong)UILabel *monthLabel;


@property (nonatomic, copy)void((^selectMonthBlock)(BOOL increase));


@end
@implementation WeeksView

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self bsl_controls];
    }
    return self;
}
- (void)bsl_controls{
    
    UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, 40)];
    [self addSubview:backgroundView];
    backgroundView.backgroundColor = [UIColor whiteColor];
    
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 60, SCREEN_WIDTH, 30)];;
    [self addSubview:bottomView];
    bottomView.backgroundColor = BY_THEME_COLOR;
    
    [backgroundView addSubview:self.lastButton];
    [backgroundView addSubview:self.nextButton];
    [backgroundView addSubview:self.yearLabel];
    
    
    [self.lastButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backgroundView).offset(15);
        make.centerY.equalTo(backgroundView.mas_centerY).offset(0);
        make.size.mas_equalTo(CGSizeMake(8, 13));
    }];
    
    [self.nextButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.lastButton.mas_right).offset(95);
        make.centerY.equalTo(backgroundView.mas_centerY).offset(0);
        make.size.mas_equalTo(CGSizeMake(8, 13));
    }];
    
    [self.yearLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(backgroundView).offset(0);
        make.centerX.equalTo(backgroundView).offset(0);
        make.size.mas_equalTo(CGSizeMake(100, 20));
    }];
    
    self.monthLabel = [[UILabel alloc] init];
    [backgroundView addSubview:self.monthLabel];
    [self.monthLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.lastButton.mas_right).offset(3);
        make.right.equalTo(self.nextButton.mas_left).offset(-3);
        make.centerY.equalTo(backgroundView).offset(0);
        make.height.mas_equalTo(@20);
    }];
    self.monthLabel.textColor = BY_THEME_COLOR;
    self.monthLabel.font = FONT(16);
    self.monthLabel.textAlignment = NSTextAlignmentCenter;
    
    
    self.selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backgroundView addSubview:self.selectBtn];
    [self.selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(backgroundView).offset(-30);
        make.centerY.equalTo(backgroundView).offset(0);
        make.size.mas_equalTo(CGSizeMake(22, 25));
    }];
    [self.selectBtn setImage:[UIImage imageNamed:@"rili"] forState:UIControlStateNormal];
    
    
    
    [self.lastButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.nextButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    NSArray *weekArray = @[@"日", @"一", @"二", @"三", @"四", @"五", @"六"];
    for (int i = 0; i < weekArray.count ; i++)  {
        NSString *title = weekArray[i];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
        label.backgroundColor = [UIColor clearColor];
        label.text = title;
        label.textColor = [UIColor grayColor];
        if (i == 0 || i == weekArray.count - 1) {
            label.textColor = [UIColor whiteColor];
        }
        label.font = [UIFont systemFontOfSize:13];
        label.textAlignment = NSTextAlignmentCenter;
        [self addSubview:label];
        [self.labsArray addObject:label];
    }
}
- (void)selectMonth:(void (^)(BOOL increase))selectBlock{
    self.selectMonthBlock = selectBlock;
}
- (void)setYear:(NSString *)year{
    _year = year;
}



- (void)setMonth:(NSInteger )month{
    _month = month;
    self.monthLabel.text = [NSString stringWithFormat:@"%@年%ld月", _year,_month];
}



- (void)buttonClick:(UIButton*)btn{
    BOOL increase = NO;
    switch (btn.tag) {
        case 100:
            increase = NO;
            break;
        case 200:
            increase = YES;
            break;
            
        default:
            break;
    }
    if (self.selectMonthBlock) {
        self.selectMonthBlock(increase);
    }
    
}

- (void)layoutSubviews{
    [super layoutSubviews];
    CGFloat w = CGRectGetWidth(self.frame);
    CGFloat h = CGRectGetHeight(self.frame);
    
    CGFloat lab_maginTop = 60;
    CGFloat lab_h =  h - lab_maginTop;

    for (int i = 0; i < self.labsArray.count; ++i) {
        UILabel *label = self.labsArray[i];
        label.frame = CGRectMake(i * (w/7.0), lab_maginTop, w/7.0, lab_h);
       
    }
    
    
    
    
    
}
- (NSMutableArray*)labsArray{
    if (!_labsArray) {
        _labsArray = [NSMutableArray arrayWithCapacity:2];
    }
    return _labsArray;
}
- (UIButton *)lastButton{
    if (!_lastButton) {
        _lastButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_lastButton setImage:[UIImage imageNamed:@"shouyejiantou"] forState:UIControlStateNormal];
        _lastButton.tag = 100;
        _lastButton.frame = CGRectZero;
    }
    return _lastButton;
}
- (UIButton *)nextButton{
    if (!_nextButton) {
        _nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_nextButton setImage:[UIImage imageNamed:@"wodexiaoxijiantou"] forState:UIControlStateNormal];
        _nextButton.tag = 200;
        _nextButton.frame = CGRectZero;
    }
    return _nextButton;
}
- (UILabel *)yearLabel{
    if (!_yearLabel) {
        _yearLabel = [[UILabel alloc]initWithFrame:CGRectZero];
        _yearLabel.textColor = [UIColor blackColor];
        _yearLabel.font = FONT(18);
        _yearLabel.textAlignment = NSTextAlignmentCenter;
        _yearLabel.text = @"我的月账本";
    }
    return _yearLabel;
}














@end
